package com.rpm.rpm.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 科研项目文件的上传
 * @author Hongduhao
 * @date : 2024/3/1
 */

public class FileUpload {
    public static void main(String[] args) {
        //用户（教师通过手动选择文件）
        String filePath = "path_to_your_file"; // 上传文件的路径

        try {
            URL url = new URL("http://example.com/upload"); // 替换为实际的上传地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            // 读取文件并上传
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = connection.getOutputStream();
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();

            int responseCode = connection.getResponseCode();
            System.out.println("Server response code: " + responseCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
